package com.flower.tokenCenter.utils;

import org.apache.commons.httpclient.*;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONObject;

/**  

* <p>Title: HttpUtil</p>  

* <p>Description:HttpUtil 工具</p>  

* @author QingFeng  

* @date 2020年5月14日  

*/  
@SuppressWarnings("deprecation")
public class HttpUtil {
	
    /**
     * post请求
     * @param url
     * @param json
     * @return
     */
    @SuppressWarnings("resource")
	public static JSONObject doPost(String url,JSONObject json){
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        JSONObject response = null;
        try {
            StringEntity s = new StringEntity(json.toString());
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");
            post.setEntity(s);
            HttpResponse res = client.execute(post);
            if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                String result = EntityUtils.toString(res.getEntity());
                response = JSONObject.parseObject(result);
            }
        } catch (Exception e) {
           doPost(url, json);
        }
        return response;
    }

}